package cn.edu.nuaa.aadl2.generatorC.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import org.eclipse.core.internal.resources.Folder;

@SuppressWarnings("restriction")
public class FileHelper {

	/* system folder create for system instance */
	// public static Folder folder = null;

	/**
	 * 初始化文件夹,如果存在则清空
	 */
	public static void initfolder(String rootPath, String folderName) {
		String path;
		if (!rootPath.endsWith("/")) {
			path = rootPath + "/" + folderName;
		} else {
			path = rootPath + folderName;
		}
//		File folder = new File(path);
//		if (folder.exists()) {
//			folder.delete();
//		}
//		folder.mkdirs();
		initfolder(path);
	}

	/**
	 * 根据绝对路径初始化文件夹,如果存在则清空
	 */
	public static void initfolder(String absoluteName) {
		File folder = new File(absoluteName);
		if (folder.exists()) {
			folder.delete();
		}
		folder.mkdirs();
	}

	/**
	 * 返回指定文件夹 如果没有就创建?
	 */
	public static File createFolder(String rootPath, String folderName) {
		String path;
		if (!rootPath.endsWith("/")) {
			path = rootPath + "/" + folderName;
		} else {
			path = rootPath + folderName;
		}
		return createFolder(path);
	}

	public static File createFolder(String absoluteName) {
		File folder = new File(absoluteName);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return folder;
	}


	/**
	 * 覆盖创建 
	 * ps 创建可写文件的时候 这里不接受绝对地址
	 */
	public static void createFile(String folderPath, String fileName, String contant) {

		File folderFile=new File(folderPath);
		if(!folderFile.exists())
			folderFile.mkdirs();
		
		String path;
		if (!folderPath.endsWith("/")) {
			path = folderPath + "/" + fileName;
		} else {
			path = folderPath + fileName;
		}
		File f = new File(path);
		try {
			if (!f.exists()) {
				f.createNewFile();
			}
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
			writer.append(contant);
			writer.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void createFile(File folder, String fileName, String contant) {
		createFile(folder.getAbsolutePath(), fileName, contant);
	}


	/**
	 * 文件尾部添加 如果没有就新键 然后添加 ps 一般用于头文件
	 */
	public static void addContent(String folderPath, String fileName, String in) {
		
		
		
		String path;
		if (!folderPath.endsWith("/")) {
			path = folderPath + "/" + fileName;
		} else {
			path = folderPath + fileName;
		}
		try {
			BufferedWriter writer  = new BufferedWriter(new FileWriter(path,true));
			writer.append(in);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
		}
		
		// IProject project = getCurrentProject();
		// IFolder folder = project.getFolder(folder1);
		// if(!folder.exists())
		// try {
		// folder.create(true, true, null);
		// } catch (CoreException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// in=in+"\n";
		// InputStream input = new ByteArrayInputStream(in.getBytes());
		// IFile newFile = folder.getFile(fileName);
		// try {
		// if (newFile.exists()) {
		// newFile.appendContents(input, 1, null);
		// newFile.refreshLocal(IResource.DEPTH_ZERO, null);
		// } else {
		// newFile.create(input, 1, null);
		// newFile.refreshLocal(IResource.DEPTH_ZERO, null);
		// }
		// } catch (CoreException e) {
		// e.printStackTrace();
		// }

	}

	/**
	 * 文件尾部添加 如果没有就新键 然后添加 ps 一般用于头文件
	 */
	public static void addContent(File folder, String fileName, String in) {
		addContent(folder, fileName, in);
	}

}
